<?php

@apache_setenv('no-gzip', 1);
    @ini_set('zlib.output_compression', 0);
    @ini_set('implicit_flush', 1);
    for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
    ob_implicit_flush(1);

include 'include/db.php';

$result = mysql_query("SELECT symbol FROM stock ORDER BY symbol");

while ($row = mysql_fetch_assoc($result)) {

    echo "Symbol: ". $row["symbol"] ."\n<br />\n";
    flush();
    
    $handle = fopen("http://finance.yahoo.com/q/hp?s=". $row["symbol"] ."+Historical+Prices", "rb");
    $contents = stream_get_contents($handle);
    fclose($handle);
    
    $doc = new DOMDocument;

    // We don't want to bother with white spaces
    $doc->preserveWhiteSpace = false;

    @$doc->LoadHTML($contents);

    $xpath = new DOMXPath($doc);

    // We starts from the root element
    $query = '//table[@id="yfncsumtab"]/tr[2]/td[1]/p/a/@href';

    $entries = $xpath->query($query);

    $csv_url = $entries->item(0)->nodeValue;
    
    if (!$csv_url) {
        echo "Not found\n<br />\n";
        flush();
        continue;
    }
    
    echo "Downloading... ";
    flush();
    
    $handle = fopen($csv_url, "rb");
    $contents = stream_get_contents($handle);
    fclose($handle);
    
    if (!$contents) {
        continue;
    }
    
    echo "Done\n<br />\n";
    flush();
    
    $lines = explode("\n", $contents);
    
    // Start on line 1, not line 0 it's a header
    array_shift($lines);
    
    echo "Found ". count($lines) ." lines\n<br />\n";
    $i=0;
    foreach ($lines as $line) {
    
        if (empty($line)) {
            continue;
        }
        
        $parts = explode(",", $line);
    
        $result2 = mysql_query("SELECT * FROM stock_data WHERE symbol = '". $row["symbol"] ."' AND date = '". $parts[0]."'");
        if (mysql_num_rows($result2)) {
            //Data already exists
        } else {
            mysql_query("INSERT INTO stock_data
            VALUES
            ('". $row["symbol"] ."','". date("Y-m-d", strtotime($parts[0])) ."',". $parts[1] .",". $parts[2] .",". $parts[3] .",". $parts[4] .",". $parts[5] .",". $parts[6] .")")
            or die(mysql_error() . "<br \>" . $line);
            echo ".";
            $i++;
            if ($i > 300) {
                $i = 0;
                echo "<br \>";
            }
        }
    }
    
    echo "<br \>";
}